import random
import re
import string
from datetime import datetime, timedelta
from dao.mqtthelp_v2 import MQTTClient
from utils.config import Config

# 模拟一辆车从入口开始往出口方向移动，每个桩口相差30S
def creatdataOneCar(path_list,lane,plate,jingweidu_list):
    num = 1
    all_vehicle_data = []
    current_time_ago=datetime.now()- timedelta(hours=0)  # 当前时间就改成0
    current_time1 = current_time_ago.strftime("%Y/%m/%d-%H:%M:%S.%f")[:-3]
    for j in range(len(path_list)):
        location=path_list[j]
        for i in range(16):
            vehicle_data = {
                'location': location[0],
                'device_num':location[3],
                'timestamp': current_time1,
                'number': 1,
                'objects_list': [{
                    'ID': 1,
                    'x': -320,
                    'y': 15000,
                    'v_x': 0,
                    'v_y': -2120,
                    'length': 480,
                    'width': 190,
                    'height': 170,
                    'type': 2,
                    'class': 0,
                    #目标类型：0 小型客车,1 中型客车,2 大型客车,3 小型货车,4 中型货车,5 重型货车,6 警
                    #车,7 救护车,8 消防车,9 校车,10 工程车,11 危化品车,12 三轮车,13 摩托车,14 自行车,15
                    #行人,16 动物,17 工作人员,100 其他
                    'color': 9,            # 车身颜色：0 白,1 灰,2 黄,3 粉,4 红,5 紫,6 绿,7 蓝,8 棕,9 黑,100 其他
                    "plate_color": 7,
                    'lane': lane,
                    'lane_dir': 2,
                    'km_dir': 1,
                    'event': 0,
                    'plate': plate,
                    'stake_mark': re.sub(r'(\w{2}\d)(\+\d{2,3})', lambda x: x.group(1) + "+" + str(int(x.group(2)[1:]) - 8*i).zfill(len(x.group(2)) - 1), location[0]),
                    'longitude': jingweidu_list[16*j+i][0],
                    'latitude': jingweidu_list[16*j+i][1],
                    'altitude': 45.00,
                    'speed': 58,
                    'heading': 49.23
                }
                #     ,{
                #     'ID': 2,
                #     'x': -320,
                #     'y': 15000,
                #     'v_x': 0,
                #     'v_y': -2120,
                #     'length': 480,
                #     'width': 190,
                #     'height': 170,
                #     'type': 2,
                #     'class': 0,
                #     'color': 9,
                #     "plate_color": 7,
                #     'lane': lane,
                #     'lane_dir': 2,
                #     'km_dir': 1,
                #     'event': 0,
                #     'plate': "湘AKK123",
                #     #"湘" + random.choice(string.ascii_uppercase[:8]) +''.join(random.choices(string.ascii_uppercase + string.digits, k=5)),
                #     'stake_mark': re.sub(r'(\w{2}\d)(\+\d{2,3})', lambda x: x.group(1) + "+" + str(int(x.group(2)[1:]) - 50).zfill(len(x.group(2)) - 1), location[0]),
                #     'longitude': location[1],
                #     'latitude': location[2],
                #     'altitude': 45.00,
                #     'speed': round(random.uniform(0, 120), 2),
                #     'heading': 49.23
                # },{
                #     'ID': 3,
                #     'x': -320,
                #     'y': 15000,
                #     'v_x': 0,
                #     'v_y': -2120,
                #     'length': 480,
                #     'width': 190,
                #     'height': 170,
                #     'type': 2,
                #     'class': 2,
                #     'color': 1,
                #     "plate_color": 7,
                #     'lane': lane+1,
                #     'lane_dir': 2,
                #     'km_dir': 1,
                #     'event': 0,
                #     'plate': "湘AKK456",
                #     #"湘" + random.choice(string.ascii_uppercase[:8]) +''.join(random.choices(string.ascii_uppercase + string.digits, k=5)),
                #     'stake_mark': re.sub(r'(\w{2}\d)(\+\d{2,3})', lambda x: x.group(1) + "+" + str(int(x.group(2)[1:]) + 50).zfill(len(x.group(2)) - 1), location[0]),
                #     'longitude': location[1],
                #     'latitude': location[2],
                #     'altitude': 45.00,
                #     'speed': round(random.uniform(0, 120), 2),
                #     'heading': 49.23
                # }
                ]
            }
            current_time1 = (current_time_ago + timedelta(seconds=0.5*num)).strftime("%Y/%m/%d-%H:%M:%S.%f")[:-3]  # 每个桩位的相隔时间
            all_vehicle_data.append(vehicle_data)
            num += 1
    return all_vehicle_data

# 实例化 MQTTClient
mqtt_client = MQTTClient()

config = Config('./conf/config.ini')
nanxian_main=config.read_value('leishi', 'nanxian_main')
nanxian_zhuru =config.read_value('leishi', 'nanxian_zhuru')
nanxian_zharu =config.read_value('leishi', 'nanxian_zharu')
nanxian_zhuchu=config.read_value('leishi', 'nanxian_zhuchu')
nanxian_zhachu=config.read_value('leishi', 'nanxian_zhachu')

beixian_main = config.read_value('leishi', 'beixian_main')
beixian_zhuru=config.read_value('leishi', 'beixian_zhuru')
beixian_zharu=config.read_value('leishi', 'beixian_zharu')
beixian_zhuchu=config.read_value('leishi', 'beixian_zhuchu')
beixian_zhachu=config.read_value('leishi', 'beixian_zhachu')

jingweidu_list=eval(config.read_value('jingweidu', 'RC031lane_5')+ ',' +config.read_value('jingweidu', 'RC030lane_5')+ ',' +config.read_value('jingweidu', 'RC029lane_5')+ ',' +config.read_value('jingweidu', 'RC028lane_5')+ ',' +config.read_value('jingweidu', 'RC025lane_5')+ ',' +config.read_value('jingweidu', 'RC024lane_5')+ ',' +config.read_value('jingweidu', 'RC023lane_5')+ ',' +config.read_value('jingweidu', 'RC022lane_5')+ ',' +config.read_value('jingweidu', 'RC021lane_5')+ ',' +config.read_value('jingweidu', 'RC020lane_5')+ ',' +config.read_value('jingweidu', 'RC019lane_5')+ ',' +config.read_value('jingweidu', 'RC018lane_5')+ ',' +config.read_value('jingweidu', 'RC017lane_5')+ ',' +config.read_value('jingweidu', 'RC016lane_5')+ ',' +config.read_value('jingweidu', 'RC015lane_5')+ ',' +config.read_value('jingweidu', 'RC014lane_5')+ ',' +config.read_value('jingweidu', 'RC013lane_5')+ ',' +config.read_value('jingweidu', 'RC012lane_5')+ ',' +config.read_value('jingweidu', 'RC008lane_5')+ ',' +config.read_value('jingweidu', 'RC007lane_5')+ ',' +config.read_value('jingweidu', 'RC006lane_5')+ ',' +config.read_value('jingweidu', 'RC005lane_5')+ ',' +config.read_value('jingweidu', 'RC004lane_5')+ ',' +config.read_value('jingweidu', 'RC003lane_5')+ ',' +config.read_value('jingweidu', 'RC002lane_5')+ ',' +config.read_value('jingweidu', 'RC001lane_5'))

# 模拟实际经纬度
path = beixian_zhuru + ',' + beixian_main + ',' + beixian_zhuchu
path_list=eval(path)
vehicle_data = creatdataOneCar(path_list,5,'湘A1266A',jingweidu_list)
print(vehicle_data)
mqtt_client.publish_multiple_messages("bizdata/objects_list", vehicle_data)
